package pl.edu.agh.mguesser.ui;

import javax.annotation.PostConstruct;
import javax.swing.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import pl.edu.agh.mguesser.ui.main.MainMenu;

/**
 * @author keiran
 */
@Controller
public class Window extends JFrame {
    
    @Autowired
    private MainMenu mainMenu;
    
    @PostConstruct
    public void postConstruct() {
        setTitle("Music Guesser");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        getContentPane().add(mainMenu);
    }
    
    public void changePanel(JPanel panel) {
        SwingUtilities.invokeLater(new PanelChangingTask(panel));
    }
    
    private class PanelChangingTask implements Runnable {

        private JPanel newPanel;

        public PanelChangingTask(JPanel newPanel) {
            this.newPanel = newPanel;
        }

        @Override
        public void run() {            
            getContentPane().removeAll();
            getContentPane().invalidate();
            
            getContentPane().add(newPanel);
            getContentPane().revalidate();
            
            getContentPane().repaint();
            
            new Thread(new Runnable() {

                @Override
                public void run() {
                    if(newPanel instanceof AwareOfBecomingActivePanel) {
                        ((AwareOfBecomingActivePanel) newPanel).becameActive();
                    }
                }
                
            }).start();
        }
        
    }

}
